package com.score.system.controller;

import com.score.system.entity.Score;
import com.score.system.entity.User;
import com.score.system.service.ScoreService;
import com.score.system.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private ScoreService scoreService;





    @RequestMapping("/doLogin")
    public ModelAndView doLogin( String username, String password, String identity, HttpSession session) {
        ModelAndView modelAndView  = null;
        System.out.println(username);
        System.out.println(password);
        System.out.println(identity);

        User user = userService.findUserByName(username);
        if(user!=null){
            //用户名存在
            String password1 = user.getPassword();
            //判断密码是否正确
            if(StringUtils.equals(password1,password)){
                //密码正确
                int flag = Integer.parseInt(identity);
                if(flag == user.getIsTeacher()){

                    session.setAttribute("isTeacher",user.getIsTeacher());
                    session.setAttribute("username", user.getUsername());

                    modelAndView = new ModelAndView("/index");


                }else{

                    modelAndView = new ModelAndView("/login");
                    modelAndView.addObject("message","请选择正确的身份登录");
                }
            }else{
                //密码不正确
                modelAndView = new ModelAndView("/login");
                modelAndView.addObject("message","密码错误");
            }
        }else{
            //用户名不存在
            modelAndView = new ModelAndView("/login");
            modelAndView.addObject("message","用户名不存在");


        }
        return  modelAndView;


    }
}
